package commMqtt

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/periodTask"
	"time"
)

type autoTask struct {
}

func (e *autoTask) TaskType() periodTask.TaskType {
	return periodTask.TaskAlone
}

func (e *autoTask) SchedulePeriod() int64 {
	return 60
}

func (e *autoTask) Execute() {
	for true {
		time.Sleep(1 * time.Second)
		e.execute()
	}
}

func (e *autoTask) execute() {
	// 定义一个函数级的异常拦截，用来防止可能的代码BUG
	defer func() {
		if r := recover(); r != nil {
		}
	}()

	keys := mgr.keys()
	for k, _ := range keys {
		client := mgr.getClient(k)
		e.reconnect(client)
	}
}

func (e *autoTask) reconnect(client *MqttClient) {
	if client == nil || client.IsConnected() {
		return
	}

	client.Connect()
}
